home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / 3dview12.zip / 3DFUNC.CPP < prev    next >
C/C++ Source or Header  |  1996-05-28  |  15KB  |  481 lines

  1. #include <stdio.h>
  2. #include <bios.h>
  3. #include <time.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #include <i86.h>
  7. #include <string.h>
  8.  
  9. #include "defines.hpp"
  10.  
  11. #include "vesa.hpp"
  12. #include "3d.hpp"
  13. #include "vesa3d.hpp"
  14. #include "misc.hpp"
  15. #include "mouse.hpp"
  16.  
  17. typedef double T3DFunction(double, double);
  18.  
  19. void ShowFunction( T3DFunction f, double X1, double Y1, double X2, double Y2,
  20.                    long m, long n, DWORD PalStart ) {
  21.     long* fPoints = new long [(m+1)*(n+1)];
  22.     double DX = (X2-X1)/n;
  23.     double DY = (Y2-Y1)/m;
  24.  
  25.     for ( long i = 0; i <= m; i++ ) {
  26.         for ( long j = 0; j <= n; j++ ) {
  27.             fPoints[i*(n+1)+j] = _3D_NewPoint( X1+j*DX, Y1+i*DY, f(X1+j*DX, Y1+i*DY) );
  28.         };
  29.     };
  30.  
  31.     for ( i = 0; i < m; i++ ) {
  32.         for ( long j = 0; j < n; j++ ) {
  33.             VESA3D_New3DQuad( fPoints[i*(n+1)+j], fPoints[i*(n+1)+j+1],
  34.                               fPoints[(i+1)*(n+1)+j+1], fPoints[(i+1)*(n+1)+j],
  35.                               PalStart, Doublesided, GouraudShadedZBuf );
  36.         };
  37.     };
  38. };
  39.  
  40. void ShowFunction_Tex( T3DFunction f, double X1, double Y1, double X2, double Y2,
  41.                         long m, long n, VESA3D_Texture &TEX ) {
  42.     long* fPoints = new long [(m+1)*(n+1)];
  43.     double DX = (X2-X1)/n;
  44.     double DY = (Y2-Y1)/m;
  45.     double TEXDX = double(TEX.TDX) / n;
  46.     double TEXDY = double(TEX.TDY) / m;
  47.  
  48.     for ( long i = 0; i <= m; i++ ) {
  49.         for ( long j = 0; j <= n; j++ ) {
  50.             fPoints[i*(n+1)+j] = _3D_NewPoint( X1+j*DX, Y1+i*DY, f(X1+j*DX, Y1+i*DY) );
  51.         };
  52.     };
  53.  
  54.     for ( i = 0; i < m; i++ ) {
  55.         for ( long j = 0; j < n; j++ ) {
  56.             _3D_NewObject(
  57.              new _3D_Triangle( fPoints[i*(n+1)+j], j*TEXDX, i*TEXDY,
  58.                                 fPoints[i*(n+1)+j+1], (j+1)*TEXDX, i*TEXDY,
  59.                                 fPoints[(i+1)*(n+1)+j+1], (j+1)*TEXDX, (i+1)*TEXDY,
  60.                                 &TEX, Doublesided, Rectangle, TexturedZBuf ));
  61.             _3D_NewObject(
  62.              new _3D_Triangle( fPoints[(i+1)*(n+1)+j+1], (j+1)*TEXDX, (i+1)*TEXDY,
  63.                                 fPoints[(i+1)*(n+1)+j], j*TEXDX, (i+1)*TEXDY,
  64.                                 fPoints[i*(n+1)+j], j*TEXDX, i*TEXDY,
  65.                                 &TEX, Doublesided, Rectangle, TexturedZBuf ));
  66.         };
  67.     };
  68. };
  69.  
  70. void ShowTitle() {
  71.     printf( "# 3D-BMP-Viewer V1.2   (w) by P.Kayser 1996 \n" );
  72.     printf( "# EMAIL : p.kayser@tu-bs.de\n" );
  73. };
  74.  
  75. void ShowHeader() {
  76.     ShowTitle();
  77.     printf( "# usage : 3DVIEW (-abdmz) <File(.BMP)> (<Mode>)\n" );
  78.     printf( "# \n" );
  79.     printf( "# options\n" );
  80.     printf( "#   -a        : show additional informations.\n" );
  81.     printf( "#   -b        : disable bitmap.\n" );
  82.     printf( "#   -d<dist>  : distance. <dist> from 0.01 (far) to 3 (near).\n" );
  83.     printf( "#   -m        : enable mouse-control.\n" );
  84.     printf( "#   -z        : enable z-buffer.\n" );
  85. };
  86.  
  87. void ShowAddInfo() {
  88.     printf( "# \n" );
  89.     printf( "# Needs VBE version 2.0.\n" );
  90.     printf( "# Only 256-color-modes allowed.\n" );
  91.     printf( "# The bitmap must be 'Windows-RGB-Encoded' and should have\n" );
  92.     printf( "# a maximum of 192 colors. Colors 192..255 are used for the\n" );
  93.     printf( "# border.\n" );
  94.     printf( "# \n" );
  95.     printf( "# 256-Color-VBE-Modes :\n" );
  96.     printf( "# 640x480 : 101 | 800x600 : 103 | 1024x768 : 105\n" );
  97. };
  98.  
  99. double Phi = 0;
  100.  
  101. double G( double x, double y ) {
  102.     double g = 1-2*x*x-4*y*y;
  103.     if ( g >= 0 )
  104.         return sqrt(g);
  105.     else
  106.         return 0;
  107. };
  108.  
  109. double J( double x, double y ) {
  110.     double g = 1-2*x*x-4*y*y;
  111.     if ( g >= 0 )
  112.         return -sqrt(g);
  113.     else
  114.         return 0;
  115. };
  116.  
  117. double H( double x, double y ) {
  118.     double h = 1-x;
  119.     return h;
  120. };
  121.  
  122. void PF( _3D_Point &P ) {
  123.     P.Z = G(P.X,P.Y);
  124. };
  125.  
  126. void main( int argc, char *argv[] ) {
  127.  
  128.     Boolean ShowBitmap = True;
  129.     Boolean ShowAddInfos = False;
  130.     Boolean WithZBuffer = False;
  131.     Boolean MouseControl = False;
  132.     double Distance = 1;
  133.     int CmdCount = 1;
  134.     char Buffer [15];
  135.  
  136.     double MinZoom = 0.01;
  137.     double MaxZoom = 3;
  138.     double NormZoom = 1;
  139.  
  140.     if ( argc == CmdCount ) {
  141.         VESA_SetTextMode();
  142.         ShowHeader();
  143.         return;
  144.     };
  145.  
  146.     while ( (argv[CmdCount])[0] == '-' ) {
  147.         int OptCount = 1;
  148.         while ( OptCount < strlen( argv[CmdCount] ) ) {
  149.             switch ( (argv[CmdCount])[OptCount] ) {
  150.                 case 'a' :
  151.                     ShowAddInfos = True;
  152.                     break;
  153.                 case 'b' :
  154.                     ShowBitmap = False;
  155.                     break;
  156.                 case 'd' :
  157.                     Distance = strtod( strncpy( Buffer,
  158.                                                 &(argv[CmdCount])[OptCount+1],
  159.                                                 strlen( argv[CmdCount] ) - OptCount ),
  160.                                        NULL );
  161.                     if ( Distance < MinZoom || Distance > MaxZoom )
  162.                         Distance = NormZoom;
  163.                     OptCount = strlen ( argv[CmdCount] );                                       
  164.                     break;
  165.                 case 'z' :
  166.                     WithZBuffer = True;
  167.                     break;
  168.                 case 'm' :
  169.                     MouseControl = True;
  170.                     break;
  171.                 default:
  172.                     VESA_SetTextMode();
  173.                     ShowTitle();
  174.                     printf( "\n" );
  175.                     printf( "ERROR : '%c' is an unknown option !\n" , argv[CmdCount][OptCount] );
  176.                     return;
  177.             }
  178.             OptCount++;
  179.         };
  180.         CmdCount++;
  181.     };
  182.  
  183.     if ( ShowAddInfos == True ) {
  184.         VESA_SetTextMode();
  185.         ShowHeader();
  186.         ShowAddInfo();
  187.         return;       
  188.     };
  189.  
  190.     static VESA3D_Texture TEX;
  191.     char* Filename = new char [256];
  192.     strcpy( Filename, argv[CmdCount] );
  193.  
  194.     if ( VESA3D_LoadTexture( Filename, TEX ) != 0 )
  195.         if ( VESA3D_LoadTexture( strcat( Filename, ".BMP" ), TEX ) != 0 ) {
  196.             VESA_SetTextMode();
  197.             ShowTitle();
  198.             printf( "\n" );
  199.             printf( "ERROR : Could not open .BMP-File !\n" );
  200.             return;
  201.     };
  202.  
  203.     VESA3D_ConvertTexture( TEX );
  204.  
  205.     CmdCount++;
  206.  
  207.     WORD Mode;
  208.     
  209.     if ( argc == CmdCount ) {
  210.         Mode = 0x0101;
  211.     }
  212.     else {
  213.         Mode = WORD( strtoul( argv[CmdCount], NULL, 16 ) );
  214.     };
  215.  
  216.     VESA_SetTextMode();
  217.     ShowTitle();
  218.     
  219.     if ( VESA_InitMode( Mode ) != 0 ) {
  220.         return;
  221.     };
  222.  
  223.     if ( VESA_BpP != 8 ) {
  224.         VESA_SetTextMode();
  225.         ShowTitle();
  226.         printf( "\n" );
  227.         printf( "ERROR: Not a 256-Color-Mode !\n" );
  228.         return;
  229.     };
  230.  
  231.     VESA3D_Init();
  232.     if ( WithZBuffer == True )
  233.         VESA3D_MakeZBuffer();
  234.  
  235.     /*
  236.     for ( int i = 47; i >= 0; i-- )
  237.         VESA_SetColor8B( WORD(239-i) ,
  238.                          WORD(32+i*5/2),
  239.                          WORD(32+i*5/2),
  240.                          WORD(32+i*5/2) );
  241.  
  242.     for ( i = 15; i >= 0; i-- )
  243.         VESA_SetColor8B( WORD(255-i),
  244.                          WORD(128+i*8),
  245.                          WORD(128+i*8),
  246.                          WORD(128+i*8) );
  247.  
  248.     VESA_SetColor8B( 0, 0, 0, 0 );
  249.  
  250.     if ( ShowBitmap == True )
  251.         VESA3D_SetTexturePalette( TEX, 192 );
  252.  
  253.     double px, py;
  254.  
  255.     if ( TEX.TDY > TEX.TDX ) {
  256.         px = 1;
  257.         py = double(TEX.TDY) / TEX.TDX;
  258.     }
  259.     else {
  260.         py = 1;
  261.         px = double(TEX.TDX) / TEX.TDY;
  262.     };
  263.  
  264.     long p1 = _3D_NewPoint(  0, py, 0 );
  265.     long p2 = _3D_NewPoint( px, py, 0 );
  266.     long p3 = _3D_NewPoint( px,  0, 0 );
  267.     long p4 = _3D_NewPoint(  0,  0, 0 );
  268.  
  269.     double dx = 0.25, dz = 0.06;
  270.  
  271.     long p5  = _3D_NewPoint( -dx, -dx, dz );
  272.     long p6  = _3D_NewPoint( -dx, -dx, -dz );
  273.     long p7  = _3D_NewPoint( px+dx, -dx, dz );
  274.     long p8  = _3D_NewPoint( px+dx, -dx, -dz );
  275.     long p9  = _3D_NewPoint( -dx, py+dx, dz );
  276.     long p10 =